<template>
    <div class="dict-type"> 
        <el-card class="!border-none" shadow="never">
            <div>
                <el-button
                    v-perms="['sys/config/add']"
                    type="primary"
                    @click="handleAdd"
                >
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <div class="mt-4" v-loading="pager.loading">
                <div>
                    <el-table
                        :data="pager.lists"
                        size="large"
                        @selection-change="handleSelectionChange"
                    > 
                        <el-table-column label="参数名称" prop="name" min-width="120" />
                        <el-table-column label="参数键名" prop="key" min-width="120" />
                        <el-table-column label="参数键值" prop="value" min-width="120" />
                        <el-table-column label="系统内置">
                            <template v-slot="{ row }">
                                <el-tag v-if="row.type == 1">是</el-tag>
                                <el-tag v-else type="danger">否</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column label="状态">
                            <template v-slot="{ row }">
                                <el-tag v-if="row.status == 1">正常</el-tag>
                                <el-tag v-else type="danger">停用</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column label="排序" prop="sort"/>
                        <el-table-column label="创建时间"   min-width="180">
                            <template #default="{ row }"> 
                                {{ $formatDt(row.createdAt) }}
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="190" fixed="right">
                            <template #default="{ row }">
                                <el-button
                                    v-perms="['sys/config/edit']"
                                    link
                                    type="primary"
                                    @click="handleEdit(row)"
                                >
                                    编辑
                                </el-button> 
                                <el-button
                                    v-perms="['sys/config/delete']"
                                    link
                                    type="danger"
                                    @click="handleDelete(row.id)"
                                >
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="flex justify-end mt-4">
                    <pagination v-model="pager" @change="getLists" />
                </div>
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script setup name="post">
import { del, getList } from '@/apis/system/config'
import { usePaging } from '@/hooks/usePaging' 
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef()
const showEdit = ref(false)
 

const { pager, getLists } = usePaging({
    fetchFun: getList, 
})

const selectData = ref([])

const handleSelectionChange = (val) => {
    selectData.value = val.map(({ id }) => id)
}

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}


const handleDelete = async (id) => {
    await feedback.confirm('确定要删除？')
    await del({ id })
    getLists()
}

onMounted(() => {
  getLists()
})
</script>
